package com.sjd.web.api.account;


import com.sjd.common.core.controller.BaseController;
import com.sjd.common.core.domain.AjaxResult;
import com.sjd.common.core.page.TableDataInfo;
import com.sjd.mall.domain.SjdAccountAddrDO;
import com.sjd.mall.domain.SjdAccountDO;
import com.sjd.mall.domain.dto.account.AccountAddressAddRequestDTO;
import com.sjd.mall.domain.dto.account.AccountAddressUpdateRequestDTO;
import com.sjd.mall.service.account.AccountAddressService;
import com.sjd.mall.service.account.AccountService;
import com.sjd.wx.utils.ThirdSessionHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
  *@CalssName AccountController
  *@Description 小程序会员账户
  *@Authot genglong
  *@Date 2022/1/11 10:16
  *@Version 1.0
  **/
@RestController
@RequestMapping("/weixin/api/ma/account")
@Slf4j
@AllArgsConstructor
@Api(value = "account", tags = "小程序账户API")
public class WxAccountApi extends BaseController {

    private final AccountService accountService;

    private final AccountAddressService accountAddressService;

    @ApiOperation(value = "查询账户信息")
    @GetMapping(value = "/queryAccount")
    public AjaxResult queryAccount() {
        SjdAccountDO sjdAccountDO =  accountService.findAccountByMemberId(ThirdSessionHolder.getSjdMemberId());
        return AjaxResult.success(sjdAccountDO);
    }

    @ApiOperation(value = "查询邮寄地址")
    @GetMapping(value = "/queryAddress")
    public TableDataInfo queryAddress() {
        startPage();
        List<SjdAccountAddrDO> list =  accountAddressService.selectAccountAddress();
        if (log.isDebugEnabled()) {
            log.debug("订单查询,响应 {}", list);
        }
        return getDataTable(list);
    }

    @ApiOperation(value = "新增邮寄地址")
    @PostMapping(value = "/insertAddress")
    public AjaxResult insertAddress(@Validated @RequestBody AccountAddressAddRequestDTO requestDTO) {
        return accountAddressService.insertAccountAddress(requestDTO);
    }

    @ApiOperation(value = "修改邮寄地址")
    @PutMapping(value = "/updateAddress")
    public AjaxResult updateAddress(@Validated @RequestBody AccountAddressUpdateRequestDTO requestDTO) {
        return accountAddressService.updateAccountAddress(requestDTO);
    }

    @ApiOperation(value = "删除邮寄地址信息")
    @PostMapping(value = "/delAddress/{addressId}")
    public AjaxResult delAddress(@PathVariable("addressId") Long addressId) {
        return accountAddressService.deleteAccountAddress(addressId);
    }
}
